package com.example.snap_it;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class ActivityComMusicaFundo extends Activity
{
	@Override
	protected void onPause()
	{
		//TocadorMusicaBackground.getInstance().pausarTocadorMusica();
		
		
		Context context = getApplicationContext();
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        if (!taskInfo.isEmpty()) {
          ComponentName topActivity = taskInfo.get(0).topActivity; 
          if (!topActivity.getPackageName().equals(context.getPackageName())) {
        	Intent iniciaMusicaFundo = new Intent(ActivityComMusicaFundo.this, BackgroundSoundService.class);
  			stopService(iniciaMusicaFundo);
            Toast.makeText(ActivityComMusicaFundo.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
          }
        }
        
        super.onPause();
		
	}
	
	@Override
	protected void onResume()
	{
		if(isMyServiceRunning() == false)
		{
			Intent iniciaMusicaFundo = new Intent(ActivityComMusicaFundo.this, BackgroundSoundService.class);
			startService(iniciaMusicaFundo);
		}
		super.onResume();
		//TocadorMusicaBackground.getInstance().resumirTocadorMusica();
		
	}
	
	private boolean isMyServiceRunning() {
	    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
	    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
	        if (BackgroundSoundService.class.getName().equals(service.service.getClassName())) {
	            return true;
	        }
	    }
	    return false;
	}
	
	

}
